(function (mui, window) {
    var v3Health = window.v3Health = {};
    var baseRequest = window.baseRequest;
    var appConfig = window.appConfig;
    var commonTool = window.commonTool;
    var commonLocale = window.dataManager.getLocale();

    var getHealthReportUrl = appConfig.v3ServiceUrl + '/health/home/health?';
    var getMiniBeiHealthUrl = appConfig.v3ServiceUrl + '/health/home/miniBei?';

    //获取场所信息
    v3Health.getHealthReport = function (acSN, callback) {
        baseRequest.commonGet(getHealthReportUrl + mui.param({
                acSN: acSN
            }), 5000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if(data == -1 || data.errcode) {
        		callback(1);
            } else {
                callback(0, mui.parseJSON(data));
            }
        });
    };

    //Add by wangyu
	var getHealthHistoryUrl = appConfig.v3ServiceUrl + '/health?';
    v3Health.healthHistory = function (devSN, mode, callback) {
        baseRequest.commonPost(getHealthHistoryUrl, {Method: "getHealthHistData",param:{devSN:devSN,mode:mode}}, 10000, function(code, data){
            		
            	if(code === 2 || code === 1) {
					return callback(2);
				}
				if(code === 0) {
					if(data.retCode === 0 || data.retCode === -1) {
						callback(0, data.message);
					} else {
						callback(1);
					}
				}
            })
    };


    //end

    /**
     * 获取小小贝健康度
     * @param apSN  设备SN
     * @param callback
     */
    v3Health.getMiniBeiHeath = function (apSN, callback) {
        baseRequest.commonGet(getMiniBeiHealthUrl + $.param({
                apSN: apSN
            }), function (code, data) {
            // code 的枚举值是0,1,2  分别是成功，失败，网络错误
            if (code === 2 || code === 1) {
                return callback(2);
            }
            //  此处code一定是=0的，所以不需要做判断
            if (data == -1 || data.errcode) {
                callback(0, {
                    "acSN": apSN,
                    "finalscore": 0,
                    "wanspeed": 0,
                    "APpercent": 0,
                    "clientspeed": 0,
                    "security": 0,
                    "wireless": 0,
                    "system": 0,
                    "Bpercent": 100
                });
            } else {
                callback(0, JSON.parse(data));
            }
        });
    };

    //  add by zhang
    var getAchievementUrl = appConfig.v3ServiceUrl + '/health/home/history/achievement?';
    v3Health.getAchievement = function (devSN, callback) {
        baseRequest.commonGet(getAchievementUrl + $.param({acSN: devSN}), function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data === '""') {
                return callback(1, commonLocale.base.err.request);
            }
            return callback(0, JSON.parse(data));
        });
    }
    //  end zhang

}(mui, window));
